package com.mujin.keji.collectionsystem.utils;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mujin.keji.collectionsystem.base.BaseVo;
import com.mujin.keji.collectionsystem.module.updata.vo.UploadListVo;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * HttpUtil 工具类，把vo转换成map
 *
 * @author weishixiong
 * @Time 2018-03-19
 */
public class HttpUtil {

    /**
     * vo转换为Map
     *
     * @param vo
     * @return
     */
    public static RequestBody convertVo2Json(BaseVo vo) {
        Map maps = convertVo2Params(vo);
        JSONObject object = new JSONObject(maps);
        return RequestBody.create(MediaType.parse("Content-Type, application/json"),
                "{\"data\":" + object.toString() + "}");
    }


    /**
     * vo转换为Map
     *
     * @param vo
     * @return
     */
    public static Map<String, String> convertVo2Params(BaseVo vo) {
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        return gson.fromJson(gson.toJson(vo), type);
    }

    /**
     * 多文件转换为 Map<String, RequestBody> bodyMap
     *
     * @param files
     * @param mediaType 文件类型
     * @return
     */
    public static Map<String, RequestBody> convertVo2BodyMap(List<File> files, String mediaType) {
        Map<String, RequestBody> bodyMap = new HashMap<>();
        for (int i = 0; i < files.size(); i++) {
            bodyMap.put("file" + i + "\"; filename=\"" + files.get(i).getName(), RequestBody.create(MediaType.parse(mediaType), files.get(i)));
        }
        return bodyMap;
    }

    /**
     * 一次性上传所有的对象文件
     * 数组对象，对象中包含文件数组
     * files转换为MultipartBody
     *
     * @param voList
     * @return
     */
    public static MultipartBody filesToMultipartBody(List<UploadListVo> voList, String mediaType) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (int i = 0; i < voList.size(); i++) {
            String applyId = "lists" + "[" + i + "]." + voList.get(i).getVisitApplyId();
            String recordTime = "lists" + "[" + i + "]." + voList.get(i).getRecordTime();
            String address = "lists" + "[" + i + "]." + voList.get(i).getVisitorAddress();
            builder.addFormDataPart(applyId, voList.get(i).getVisitApplyId());
            builder.addFormDataPart(recordTime, voList.get(i).getRecordTime());
            builder.addFormDataPart(address, voList.get(i).getVisitorAddress());
            //
            for (int j = 0; j < voList.get(i).getuFile().size(); j++) {
                //  这里为了简单起见，没有判断file的类型
                RequestBody requestBody = RequestBody.create(MediaType.parse(mediaType), voList.get(i).getuFile().get(j));
                builder.addFormDataPart("lists" + "[" + i + "]." + "uFile", voList.get(i).getuFile().get(j).getName(), requestBody);
            }

        }
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    /**
     * 上传单个对象的文件
     * 对象中包含文件数组
     * files转换为MultipartBody
     *
     * @param files
     * @return
     */
    public static MultipartBody filesToMultipartBody(BaseVo vo, List<File> files, String mediaType) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (File file : files) {
            //  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse(mediaType), file);
            builder.addFormDataPart("files", file.getName(), requestBody);

        }
        if (EmptyUtils.isNotEmpty(vo)) {
            Map<String, String> map = HttpUtil.convertVo2Params(vo);
            for (String key : map.keySet()) {

                builder.addFormDataPart(key, map.get(key));
            }
        }
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    /**
     * @param files
     * @return
     */
    public static MultipartBody filesToMultipartBodyParts(String fileName, List<File> files, String mediaType) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (File file : files) {
            //  这里为了简单起见，没有判断file的类型
            //  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse(mediaType), file);
            builder.addFormDataPart(fileName, file.getName(), requestBody);
        }
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

}
